﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public class TestFormScriptCallback : ScriptCallback
    {
        public TestFormScriptCallback(FormBase bindingForm) : base(bindingForm) { }        

        public string GetMessage(string arg)
        {
            //MessageBox.Show(arg);

            dynamic obj = new System.Dynamic.ExpandoObject();
            obj.id = 2;
            obj.name = "yaowt";
            obj.text = "姚文涛";

            string data = JsonConvert.SerializeObject(obj);
            return data;
        }

        public string GetJsonData(object jsonData)
        {
            MessageBox.Show(jsonData.ToString());
            return "GetJsonData";
        }

        public void GotoPage(string pageName)
        {
            BindingForm.HtmlFile = pageName;
        }

        public async void CallScriptFunc()
        {
            //调用html文件中的js脚本并传递json字符串参数
            dynamic obj = new System.Dynamic.ExpandoObject();
            obj.id = 2;
            obj.name = "yaowt";
            obj.text = "姚文涛";

            await BindingForm.ExecuteScriptAsync("showMessage", obj);
        }
    }
}
